<template>
  <list-scroll-view
    v-model="refreshing"
    refresher-enabled
    @refresh="onRefresh"
    @load="onListLoad"
  >
    <column
      v-for="(item,index) in list"
      :key="index"
      :format="item.format"
      :title="item.title"
      :description="item.description"
      :medias="item.medias"
      @click="pushNativelink(item.mplink)"
    />
  </list-scroll-view>
</template>
<script>
import ListScrollView from '@/components/listScrollView/index'
import Column from '@/components/column/index'
import MPRequest from '@/utils/request'
export default {
  components: {
    Column,
    ListScrollView
  },
  props: {
    tab: {
      type: Object,
      default: null
    },
    url: {
      type: String,
      default: ''
    },
    params: {
      type: Object,
      default: null
    },
    className: {
      type: String,
      default: ''
    },
    scroll: {
      type: Object,
      default: {
        x: false,
        y: true
      }
    }
  },
  data () {
    return {
      channelId: '',
      list: [],
      refreshing: false,
      page: 1, // 当前页数
      meta: {
        totalCount: 0,
        pageCount: 0,
        currentPage: 0,
        perPage: 0
      }
    }
  },
  onLoad (option) {
    console.log('onLoad options:', option)
    if (option.id) {
      this.channelId = parseInt(option.id)
    }
  },
  beforeMount () {
    console.log('url::::', this.url)
    console.log('params::::', this.params)
    this.getList()
  },
  methods: {
    getList () {
      // 获取数据方法
      const that = this
      that.refreshing = false
      let startTime = new Date()
      const params = Object.assign({}, that.params, {
        page: that.page // 传递页码
      })
      MPRequest.get(this.url, params).then(res => {
        // 返回的数据
        // 关闭下拉刷新动画
        wx.stopPullDownRefresh()
        let itemCount = res.items.length
        if (itemCount > 0) {
          if (that.page > 1) {
            that.list.push(...res.items)
          } else {
            that.list = res.items
          }
        }
        that.meta = res.meta

        let endtime = new Date()
        let duration = endtime - startTime
        if (duration < 1500) {
          setTimeout(() => {
            that.refreshing = true
          }, 1500 - duration)
        } else {
          that.refreshing = true
        }
        // that.triggered = false
        // that.freshing = false
      }).catch(e => {
        console.error(e)
      })
    },
    onRefresh () {
      // 下拉刷新
      this.page = 1
      this.getList()
    },
    // 上拉加载
    onListLoad () {
      // 执行上拉执行的功能
      if (this.meta.pageCount <= this.meta.currentPage) {
        console.log('数据加载完了')
      } else {
        // 下一页
        this.page = this.page + 1
        this.getList()
      }
    }
  }
}
</script>
